package com.fxcomponents.mp3player
{
	import com.fxcomponents.Component;
	
	import flash.display.Shape;
	import flash.events.MouseEvent;
	
	public class Button extends Component
	{
		public function Button()
		{
			super();
			
			buttonMode = true;
			useHandCursor = true;
			
			addEventListener(MouseEvent.ROLL_OVER, onRollOver);
			addEventListener(MouseEvent.ROLL_OUT, onRollOut);
		}
		
		protected var fill:Shape;
		protected var icon:Shape;
		
		private var _color:uint = 0xffffff;
		private var colorChanged:Boolean;
		
		public function set color(value:uint):void
		{
			_color = value;
			colorChanged = true;
			
			invalidateDisplayList();
		}
		
		private var _rollOverColor:uint = 0xff0000;
		private var rollOverColorChanged:Boolean;
		
		public function set rollOverColor(value:uint):void
		{
			_rollOverColor = value;
			rollOverColorChanged = true;
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			fill = new Shape();
			addChild(fill);
			
			icon = new Shape();
			addChild(icon);
			
			fill.mask = icon;
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			graphics.clear();
			graphics.beginFill(0, 0);
			graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
			
			if(colorChanged)
			{
				fill.graphics.clear();
				fill.graphics.beginFill(_color);
				fill.graphics.drawRect(0, 0, unscaledWidth, unscaledHeight)
				
				colorChanged = false;
			}
		}
		
		private function onRollOver(e:MouseEvent):void
		{
			trace("Button -> onRollOver");
			fill.graphics.clear();
			fill.graphics.beginFill(_rollOverColor);
			fill.graphics.drawRect(0, 0, unscaledWidth, unscaledHeight)
		}
		
		private function onRollOut(e:MouseEvent):void
		{
			trace("Button -> onRollOut");
			fill.graphics.clear();
			fill.graphics.beginFill(_color);
			fill.graphics.drawRect(0, 0, unscaledWidth, unscaledHeight)
		}
		
		protected function centerIcon():void
		{
			icon.x = int((unscaledWidth - icon.width)/2);
			icon.y = int((unscaledHeight - icon.height)/2);
		}
	}
}